package com.smartcampus.demo.service;

import com.smartcampus.demo.domain.User;
import com.smartcampus.demo.repository.UserDao;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;


    @Override
    public User loginService(String username, String password) {
        User user = userDao.findByUsernameAndPassword(username, password);
        if (user != null) {
            user.setPassword(null);
        }
        return user;
    }

    @Override
    public User registerService(User user) {
        if(userDao.findByUsername(user.getUsername()) != null){
            return null;
        }
        else {
            User newUser = userDao.save(user);
            if(newUser != null){
                newUser.setPassword(null);
            }
            return newUser;
        }
    }
}
